package xyz.xiangyushui.dao.employee;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import xyz.xiangyushui.dao.Dao;

public class EmployeeDao extends Dao{
	
	public void addEmployee(String userName){
		db.executeUpdate("insert into employee values(?,?)",new Object[]{userName,0} );
	}
	
	public boolean isAssignedToStore(String userName){
		boolean has=false;
		List<Object> list=db.excuteQuery("select storeId from employee where userName=?", new Object[]{userName});
		if(!list.isEmpty()){
			@SuppressWarnings("unchecked")
			Map<String, Object> map = (Map<String, Object>) list.get(0);
			if((int)map.get("storeId")!=0){
				has=true;
			}
		}
		return has;
	}
	
	public int getStoreId(String userName){
		int storeId=0;
		List<Object> list=db.excuteQuery("select storeId from employee where userName=?", new Object[]{userName});
		if(!list.isEmpty()){
			@SuppressWarnings("unchecked")
			Map<String, Object> map = (Map<String, Object>) list.get(0);
			storeId=(int)map.get("storeId");
		}
		return storeId;
	}
	
	public List<String> getAllEmployees(){
		List<String> employees=new LinkedList<>();
		List<Object> list=db.excuteQuery("select userName from employee ", new Object[]{});
		if(!list.isEmpty()){
			for(Object o:list){
				@SuppressWarnings("unchecked")
				Map<String,Object> m=(Map<String, Object>) o;
				employees.add((String) m.get("userName"));
			}
		}
		return employees;
		
	}
}
